﻿using BulkUploadGovernanceFiles.Models;
using BulkUploadGovernanceFiles.MongoSpecification;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;

namespace BulkUploadGovernanceFiles.Repositories
{
    public class GovernanceCompanyProfileRepository
    {
        private readonly MongoCollection<GovernanceCompanyProfile> _governanceCompanyProfileCollection;

        public GovernanceCompanyProfileRepository()
        {
            var corporateDataConnectionString = ConfigManager.GovernanceDw.ConnectionString;

            var mongoClient = new MongoClient(corporateDataConnectionString);
            var server = mongoClient.GetServer();
            var database = server.GetDatabase(ConfigManager.GovernanceDw.DataBaseName);

            _governanceCompanyProfileCollection = database.GetCollection<GovernanceCompanyProfile>("GovernanceCompanyProfile");
        }

        public GovernanceCompanyProfile Get(ISpecification<GovernanceCompanyProfile> specification)
        {
            return _governanceCompanyProfileCollection.AsQueryable<GovernanceCompanyProfile>().FirstOrDefault(specification.SpecExpression);
        }

        public void Update(GovernanceCompanyProfile companyProfile)
        {
            _governanceCompanyProfileCollection.Save(companyProfile);
        }
    }
}
